﻿using System;
using System.Collections.Generic;
using System.Linq;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class ManufacturerBiz
    {
        #region Properties

        private readonly IManufacturerGroupDao _manufacturerGroupDao;
        private readonly IManufacturerDao _manufacturerDao;

        #endregion

        #region Constructors

        public ManufacturerBiz()
        {
            _manufacturerGroupDao = new ManufacturerGroupDao();
            _manufacturerDao = new ManufacturerDao();
        }

        public ManufacturerBiz(IManufacturerGroupDao manufacturerGroupDao, IManufacturerDao manufacturerDao)
        {
            _manufacturerGroupDao = manufacturerGroupDao;
            _manufacturerDao = manufacturerDao;
        }

        #endregion

        #region Methods

        public List<ManufacturerGroup> GetAllManufacturerGroups()
        {
            try
            {
                return _manufacturerGroupDao.GetAllManufacturerGroups();
            }
            catch(Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
            return new List<ManufacturerGroup>();
        }

        public Manufacturer GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _manufacturerDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Manufacturer CreateManufacturer(Manufacturer manufacturer)
        {
            if (manufacturer == null) return null;
            if (string.IsNullOrEmpty(manufacturer.Code) || string.IsNullOrEmpty(manufacturer.Name)) return null;

            try
            {
                var addedManufacturer = _manufacturerDao.AddManufacturer(manufacturer);
                return addedManufacturer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public Manufacturer UpdateManufacturer(Manufacturer manufacturer)
        {
            if (manufacturer == null) return null;

            try
            {
                _manufacturerDao.UpdateManufacturer(manufacturer);
                return manufacturer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _manufacturerDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _manufacturerDao.GetCurrentIdentity() + 1;
        }

        public object GetManufacturerGroupOptionsWs()
        {
            try
            {
                var groups = _manufacturerGroupDao.GetAllManufacturerGroups();//.Select(g => new { DisplayText = g.Name, Value = g.Id });
                groups.Insert(0, new ManufacturerGroup { Id = 0, Name = "" });
                return new { Result = "OK", Options = groups.Select(g => new { DisplayText = g.Name, Value = g.Id }) };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object ManufacturerListByFilterWs(string name, int manufacturerGroupId, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                var manufacturerCount = _manufacturerDao.GetManufacturerCountByFilter(name, manufacturerGroupId);
                var manufacturers = _manufacturerDao.GetManufacturersByFilter(name, manufacturerGroupId, jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = manufacturers, TotalRecordCount = manufacturerCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateManufacturerWs(Manufacturer manufacturer)
        {
            if (manufacturer == null) return null;
            if (string.IsNullOrEmpty(manufacturer.Code) || string.IsNullOrEmpty(manufacturer.Name)) return null;

            try
            {
                var addedManufacturer = _manufacturerDao.AddManufacturer(manufacturer);
                return new { Result = "OK", Record = addedManufacturer };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateManufacturerWs(Manufacturer manufacturer)
        {
            if (manufacturer == null) return null;

            try
            {
                _manufacturerDao.UpdateManufacturer(manufacturer);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteManufacturerWs(int manufacturerId)
        {
            try
            {
                _manufacturerDao.DeleteManufacturer(manufacturerId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers
        #endregion
    }
}
